package com.bdth.mixapi.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Random;

/**
 * Created by Administrator on 2019/4/11.
 */
public class DateUtil {
    /**
     * 获取上一天的日期
     * @return
     */
    public static String getLastDate(){
        Calendar ca = Calendar.getInstance();//得到一个Calendar的实例
        ca.setTime(new Date()); //设置时间为当前时间
        ca.add(Calendar.DATE, -1);
        Date lastDate = ca.getTime(); //结果
        //求前一月ca.add(Calendar.MONTH, -1)，
        //前一天ca.add(Calendar.DATE, -1)
        //年份减1ca.add(Calendar.YEAR, -1)
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(lastDate);
    }

    /**
     * 获取上一天的日期
     * @return
     */
    public static String getLastDate(String patter){
        Calendar ca = Calendar.getInstance();//得到一个Calendar的实例
        ca.setTime(new Date()); //设置时间为当前时间
        ca.add(Calendar.DATE, -1);
        Date lastDate = ca.getTime(); //结果
        //求前一月ca.add(Calendar.MONTH, -1)，
        //前一天ca.add(Calendar.DATE, -1)
        //年份减1ca.add(Calendar.YEAR, -1)
        SimpleDateFormat sdf = new SimpleDateFormat(patter);
        return sdf.format(lastDate);
    }

    /**
     * 获取上N天的日期
     * @param  day 表示前N天
     * @return
     */
    public static String getLastDate(int day){
        Calendar ca = Calendar.getInstance();//得到一个Calendar的实例
        ca.setTime(new Date()); //设置时间为当前时间
        ca.add(Calendar.DATE, -1*day);
        Date lastDate = ca.getTime(); //结果
        //求前一月ca.add(Calendar.MONTH, -1)，
        //前一天ca.add(Calendar.DATE, -1)
        //年份减1ca.add(Calendar.YEAR, -1)
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(lastDate);
    }

    /**
     * 获取上N天的日期
     * @param  day 表示前N天
     * @return
     */
    public static String getLastDate(int day, String patter){
        Calendar ca = Calendar.getInstance();//得到一个Calendar的实例
        ca.setTime(new Date()); //设置时间为当前时间
        ca.add(Calendar.DATE, -1*day);
        Date lastDate = ca.getTime(); //结果
        //求前一月ca.add(Calendar.MONTH, -1)，
        //前一天ca.add(Calendar.DATE, -1)
        //年份减1ca.add(Calendar.YEAR, -1)
        SimpleDateFormat sdf = new SimpleDateFormat(patter);
        return sdf.format(lastDate);
    }

    public static String getRandomDateTime(String patter){
        Random ran=new Random();
        int second = ran.nextInt(30);
        Calendar ca = Calendar.getInstance();//得到一个Calendar的实例
        ca.setTime(new Date()); //设置时间为当前时间
        ca.add(Calendar.SECOND, -1*second);
        Date lastDate = ca.getTime(); //结果
        SimpleDateFormat sdf = new SimpleDateFormat(patter);
        return sdf.format(lastDate);
    }

    public static String format(Date date,String patter){
        SimpleDateFormat sdf = new SimpleDateFormat(patter);
        return sdf.format(date);
    }

    public static String format(Date date){
        return format(date,"yyyy-MM-dd");
    }

    public static Date toDate(String date, String patter){
        SimpleDateFormat sdf = new SimpleDateFormat(patter);
        try {
            return sdf.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return new Date();
    }

    public static Date toDate(String date){
        return toDate(date,"yyyy-MM-dd");
    }

    public static void main(String[] arg){
        System.out.println(toDate("2019-04-30"));
    }
}
